/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.phoenix.repository.dao.mysql;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;

import net.phoenix.repository.dao.NodeTableInsertDao;
import net.phoenix.repository.data.NodeData;

/**
 * Jigsaw server
 * 
 * @author    <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date      2012-11-15
 * @version   1.0.0
 */
public class MysqlNodeTableInsertDao extends NodeTableInsertDao{
	/**
	 * 插入新结点
	 * 
	 * @param data
	 *            NodeData
	 * @throws SQLException
	 */
	private static final String SQL_INSERT = "insert into NODE (PKID, PARENT_ID, NAME,VINDEX, PRIMARYType,`MIXINTYPES[0]`, `MIXINTYPES[1]`,`MIXINTYPES[2]`,`MIXINTYPES[3]`,`MIXINTYPES[4]`,`MIXINTYPES[5]`,`MIXINTYPES[6]`,`MIXINTYPES[7]`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";

	public void insert(NodeData data) throws SQLException {
		if (data.getID() == null)
			data.setID("" + getNextID());
		PreparedStatement command = null;
		try {
			command = connection.prepareStatement(SQL_INSERT);
			command.setString(1, data.getID());
			command.setString(2, data.getParentID());
			command.setString(3, data.getName());
			command.setInt(4, data.getOrderNo());
			command.setString(5,data.getPrimaryType());
			int i =0;
			for(i=0; i< data.getMixinTypes().size(); i++)
				command.setString(i+6, data.getMixinTypes().get(i));
			for(;i<8;i++) {
				command.setNull(i+6, Types.VARCHAR);
			}
			command.executeUpdate();
		} finally {
			this.close(command);
		}
		data.setNew(false);
	}
}
